package cn.lanyi.org.controller;

import cn.lanyi.basic.util.AjaxResult;
import cn.lanyi.basic.util.PageList;
import cn.lanyi.org.domain.Employee;
import cn.lanyi.org.query.EmployeeQuery;
import cn.lanyi.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    @GetMapping ()
    public List<Employee> findAllEmployees(){
        return employeeService.loadAll();
    }

    @PutMapping()
    public AjaxResult saveEmployee(@RequestBody Employee employee) {
        try {
            if (null != employee.getId()) {// 修改
                employeeService.update(employee);
            } else {// 新增
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteEmployeeById(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试");
        }
    }

    @PostMapping
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryData(query);
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请重试");
        }
    }
}
